# Stage 1: Build the application
FROM golang:1.22-alpine AS builder

# Set necessary environment variables
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

# Set the working directory
WORKDIR /app

# Copy go.mod and go.sum to download dependencies first
# This leverages Docker's layer caching
COPY go.mod go.sum ./
RUN go mod download

# Copy the rest of the source code
COPY . .

# Build the application, creating a statically linked binary.
# -ldflags "-w -s" strips debug information and symbols to reduce the binary size.
RUN go build -ldflags="-w -s" -o /app/mcp-db-gateway ./cmd/server

# Stage 2: Create the final, minimal image
FROM alpine:latest

# Set the working directory
WORKDIR /app

# Copy the built binary from the builder stage
COPY --from=builder /app/mcp-db-gateway .

# Copy the configuration file
COPY config.yaml .

# Expose the port the app runs on (defined in config.yaml)
EXPOSE 8080

# The SQLite database file will be created in the working directory.
# For persistent storage, you can mount a volume to /app
# e.g., docker run -p 8080:8080 -v my_app_data:/app my-gateway-image
# This will persist gateway.db across container restarts.

# Command to run the application
CMD ["./mcp-db-gateway"] 